package com.cs.manaer.service;

import com.cs.manaer.common.utils.RedisSerializableUtils;
import com.cs.manaer.controller.req.RoleReqMsg;
import com.cs.manaer.controller.req.UserRoleReqMsg;
import com.cs.manaer.controller.res.ListCsmgRoleResMsg;
import com.cs.manaer.dao.CsmgRoleMapper;
import com.cs.manaer.po.CsmgRole;
import com.lin.common.constant.CommonConstant;
import com.lin.common.error.CustomRuntimeException;
import com.lin.common.utils.BeanCopyUtils;
import com.lin.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RoleServiceImpl implements RoleService {
    @Autowired
    private CsmgRoleMapper csmgRoleMapper;

    @Override
    public Integer userAddRole(UserRoleReqMsg reqMsg) {
        return null;
    }

    @Override
    public ListCsmgRoleResMsg listrole(Integer pageSize, Integer pageNo) {
        ListCsmgRoleResMsg lcr = new ListCsmgRoleResMsg();
        lcr.setRecords(csmgRoleMapper.listrole(pageSize, pageNo));
        lcr.setTotal(csmgRoleMapper.listrolecount());
        return lcr;
    }

    @Override
    public Integer addrole(RoleReqMsg reqMsg) throws CustomRuntimeException {
        CsmgRole cr = BeanCopyUtils.beanCopy(reqMsg, CsmgRole.class);
        cr.setRoleId(Long.parseLong(RedisSerializableUtils.generateForLong(CommonConstant.DEFAULT_LENGTH)));
        cr.setCreateTime(DateUtils.fullDateFormat());
        cr.setUpdateTime(DateUtils.fullDateFormat());
        return csmgRoleMapper.insertSelective(cr);
    }

    @Override
    public Integer delrole(Long id) {
        return csmgRoleMapper.deleteByPrimaryKey(id);
    }
}
